<template>
  <div>
    <van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  :immediate-check="false"
  @load="onLoad"
>
  <commontItem v-for="item in list" :key="item.com_id" :item="item" @click-reply="$emit('click-reply', $event)"/>
  <!-- <van-cell v-for="item in list" :key="item.art_id" :title="item.title" /> -->
</van-list>
  </div>
</template>

<script>
import { getCommont } from '@/api/article'
import commontItem from '@/components/commont-item'
export default {
  data () {
    return {
      // list: [],
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },
  props: {
    articleId: {
      type: [String, Object, Number],
      required: true
    },
    list: {
      type: Array,
      default: () => []
    },
    type: {
      type: String,
      default: 'a'
    }
  },
  components: {
    commontItem
  },
  created () {
    this.loading = true
    this.onLoad()
  },
  computed: {},
  methods: {
    async onLoad () {
    //   1.获取数据
      try {
        const { data } = await getCommont({
          type: this.type,
          source: this.articleId,
          offset: this.offset,
          limit: this.limit
        })
        // 2.渲染数据
        const { results } = data.data

        // eslint-disable-next-line vue/no-mutating-props
        this.list.push(...results)
        // 将数据传递至父组件
        this.$emit('loadingCommont', data.data)
        // 3.加载状态设置为false
        this.loading = false
        // 4.设置判断条件，完成加载
        if (results.length) {
          this.offset = data.data.last_id
        } else {
          this.finished = true
        }
        console.log(data)
      } catch (err) {
        this.$toast.fail('获取文章评论失败')
      }
    }
  }
}
</script>

<style lang='less' scoped>
</style>
